package com.character.controller;



import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.character.entity.MyReturn;
import com.character.entity.Question;
import com.character.service.QuestionService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 问题表(Question)表控制层
 *
 * @author makejava
 * @since 2021-11-26 16:12:49
 */
@RestController
@RequestMapping("question")
public class QuestionController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private QuestionService questionService;

    /**
     * 分页查询所有数据
     *
     * @param
     * @return 所有数据
     */
    @GetMapping
    public MyReturn selectAll(Integer limit,Integer page,Question question) {
        Page<Question> questionPage = new Page<>(page,limit);
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(question.getQuestion())){
            queryWrapper.like("question",question.getQuestion());
        }
        if(ObjectUtil.isNotEmpty(question.getCreator())){
            queryWrapper.like("creator",question.getCreator());
        }
        Page<Question> page1 = this.questionService.page(questionPage,queryWrapper);
        return super.success(page1.getRecords(),page1.getTotal());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectQuestion")
    public MyReturn selectOne(String id) {
        Question question = this.questionService.getById(id);
        return super.success(question,1L);
    }

    /**
     * 新增数据
     *
     * @param question 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    public MyReturn insert(@RequestBody Question question) {
        question.setCreateTime(new Date());
        boolean save = questionService.save(question);
        if(save){
            return success(question,1L);
        }else {
            return failed(question,1L);
        }
    }

    /**
     * 修改数据
     *
     * @param question 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    public MyReturn update(@RequestBody Question question) {
        question.setUpdateTime(new Date());
        boolean updateById = questionService.updateById(question);
        if(updateById){
            return super.success(question,1L);
        }else {
            return super.failed(question,1L);
        }
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    public MyReturn delete(@RequestParam("idList[]")List<Long> idList) {
        boolean b = questionService.removeByIds(idList);
        if(b){
            return super.success(idList);
        }else {
            return super.failed(idList);
        }
    }
    /**
     *
     * @param
     * @return com.character.entity.MyReturn
     * @author 高冷
     * @description:查询所有问题,用于测试题页面,不需要分页,状态正常的才能被查询
     * @date 2021/12/3 9:20
     */
    @RequestMapping("/getAllTestQuestion")
    public MyReturn getAllTestQuestion(){
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",0);
        List<Question> list = questionService.list(queryWrapper);
        return success(list,list.stream().count());
    }
}

